package com.xywy.bloodandroid.act;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.base.BasicAdapter;
import com.xywy.bloodandroid.custom.OwnListView;
import com.xywy.bloodandroid.custom.OwnListView.OnTouchingLetterChangedListener;
import com.xywy.bloodandroid.util.Constants;

public class SetProvinceAct extends BaseActivity {

	private RelativeLayout left_img;
	private OwnListView indexBar;
	
	private HashMap<String, Integer> alphaIndex = new HashMap<String, Integer>();// 存放存在的汉语拼音首字母和与之对应的列表位置
	private String[] strings;// 存放存在的汉语拼音首字母
	private WindowManager mWindowManager;
	private WindowManager.LayoutParams lp;
	private TextView txtOverlay;
    private int num ;
	private ListView lv_province;
	private String value;
	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_select_province);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		TextView middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(R.string._set_province);
		indexBar = (OwnListView) findViewById(R.id.sideBar);
		
		lv_province = (ListView) findViewById(R.id.lv_province);
		
		ProvinceAdapter adapter=new ProvinceAdapter(context, Constants.province);
		lv_province.setAdapter(adapter);
		
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		indexBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
			
			@Override
			public void onTouchingLetterChanged(String s) {
				
			}
		});
	}
 
	@Override
	protected void initData() {
		initOverLay();
	}
	
	
	// 初始化汉语拼音首字母弹出提示框
		private void initOverLay() {
			LayoutInflater inflater = LayoutInflater.from(this);
			txtOverlay = (TextView) inflater.inflate(
					R.layout.province_list_postion, null);
			txtOverlay.setVisibility(View.INVISIBLE);
			lp = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT,
					WindowManager.LayoutParams.TYPE_APPLICATION,
					WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
							| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
					PixelFormat.TRANSLUCENT);
			mWindowManager = (WindowManager) this
					.getSystemService(Context.WINDOW_SERVICE);
			mWindowManager.addView(txtOverlay, lp);
		}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			Finish("");
			break;

		default:
			break;
		}
	}
	
	
	private void  Finish(String value){
		Intent date = new Intent();
		date.putExtra("province", value);
		setResult(3, date);
		SetProvinceAct.this.finish();
	};

	private class ProvinceAdapter extends BasicAdapter<String> implements SectionIndexer{
		private LayoutInflater inflater;
		public ProvinceAdapter(Context context, List<String> list) {
			super(context, list);
			this.inflater = LayoutInflater.from(context);
		}
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			viewholder holder ;
			if(convertView==null){
				holder=new viewholder();
				convertView=inflater.inflate(R.layout.item_select_province, null);
				holder.tv_letter=(TextView) convertView.findViewById(R.id.tv_letter);
				holder.tv_province=(TextView) convertView.findViewById(R.id.tv_province);
				convertView.setTag(holder);
			}else{
				holder=(viewholder) convertView.getTag();
			}
			
//			strings = new String[Constants.province.size()];
//			
//			String currentLetter = "";
//			String lastLetter = "";
////			for(int i=0;i<Constants.province.size();i++){
//				
//				String lastname = (i - 1) >= 0 ? (String) Constants.province.get(i - 1) : null;
//				
//			char letter = PingYinUtil
//					.getPinYinHeadChar(
//							Constants.profession.get(i))
//					.toUpperCase().substring(0, 1).charAt(0);
//			currentLetter = (letter + "").toUpperCase();
//			
//			if (!"".equals(lastname) && lastname != null) {
//				lastLetter = (position - 1) >= 0 ? (PingYinUtil
//						.getPinYinHeadChar(
//								Constants.province.get(position - 1).toUpperCase()
//						.substring(0, 1).charAt(0) + "").toUpperCase()): "";
//			} else {
//				lastLetter = "";
//			}
//			if (!lastLetter.equals(currentLetter)) {
//				if (Pattern.matches("[A-Z]", currentLetter)) {
//					System.out.println("currentLetter" + currentLetter);
//					num = position;
//				}
//				alphaIndex.put(currentLetter, position - num);
//				strings[position] = currentLetter;
//			}
//			
//			
//			holder.tv_letter.setText(currentLetter);
//			holder.tv_province.setText(Constants.province.get(position));
//			// 首字母不为数字
//				if (!lastLetter.equals(currentLetter)) {// 首字母不相等，创建新的字母集合
//					holder.tv_letter.setText(currentLetter);
//					holder.tv_letter
//							.setVisibility(View.VISIBLE);
//				} else {// 首字母相等，则放在一起
//					holder.tv_letter.setVisibility(View.GONE);
//				}
			
//			}
//			holder.tv_letter.setText(currentLetter);
//			holder.tv_letter
//					.setVisibility(View.VISIBLE);
			convertView.setOnClickListener(new OnClickListener() {
				
				

				@Override
				public void onClick(View v) {
					
					value = Constants.province.get(position);
					Finish(value);
				}
			});
			holder.tv_province.setText(Constants.province.get(position));
			
			
			return convertView;
		}
		
		
		
		private class viewholder{
			private TextView  tv_letter;
			private TextView tv_province;
		}


		
		@Override
		public Object[] getSections() {
			return null;
		}
		@Override
		public int getPositionForSection(int section) {
			return 0;
		}
		@Override
		public int getSectionForPosition(int position) {
			return 0;
		}
	}
}
